/**
 * New
 * 
 * 11/10/2013
 * 
 * Copyright GPL
 */
package info.victor.servlets;

import info.victor.utils.AccessDB;
import info.victor.utils.StringUtils;
import info.victor.utils.WebUtils;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * This class is for creating a tale.
 * Servlet implementation class New
 */
@WebServlet("/New")
public class New extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
    /**
     * constructor
     * @see HttpServlet#HttpServlet()
     */
    public New() {
        super();
    }

	/**
	 * redirects to new.jsp. Wrong way!
	 * @see HttpServlet#doGet(HttpServletRequest request, 
	 * HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException,
					IOException {
		
		WebUtils.redirect(this, request, "new.jsp", response);
	}

	/**
	 * this method receives the parameters (title and tale) and it inserts into the DB
	 * @see HttpServlet#doPost(HttpServletRequest request, 
	 * HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, 
			HttpServletResponse response) throws ServletException, 
					IOException {
		
		String title = request.getParameter("title");
		String tale = request.getParameter("tale");
		
		if(StringUtils.checkVoidStrings(title, tale)){
			if(new AccessDB().insertTaleIntoDB(title, tale)){
				WebUtils.redirect(this, request, "/Home", response);
			}else{
				WebUtils.redirect(this, request, "/new.jsp", response);
			}
		}else{
			WebUtils.redirect(this, request, "/new.jsp", response);
		}
	}

}
